package ee.ut.cs.mc.madp.p2pdivider;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Client activity that receives data and displays it
 * 
 * @author Kaarel Jõgeva
 */
public class ClientActivity extends BaseDividerActivity implements PeerListListener {

    public static final String INTENT = "p2p.media.divider.CLIENT";
    public static final String TAG = "ClientActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);
        Toast.makeText(getApplicationContext(), R.string.info_wait_host_add, Toast.LENGTH_LONG).show();
        SERVERIP = getLocalIpAddress();
    }

    @Override
    public void showFile(String name) {
        ImageView imageView = (ImageView) findViewById(R.id.dividedClientImage);

        FileInputStream image = null;
        try {
            image = openFileInput(name);

            // Get display size
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

            // Bitmap decodedBitmap = Utils.decodeSampledBitmapFromResource(image, metrics.widthPixels, metrics.heightPixels, Area.S);
            // imageView.setImageBitmap(decodedBitmap);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Unable to lacate file " + name, e);
            say("Couldn't display file " + name);
        } finally {
            Utils.closeQuietly(image);
        }
    }

    // FIXME LIFECYCLE

    public String getLocalIpAddress() {

        WifiManager wim = (WifiManager) getSystemService(WIFI_SERVICE);
        List<WifiConfiguration> l = wim.getConfiguredNetworks();
        if (l.isEmpty()) {
            return null;
        }
        WifiConfiguration wc = l.get(0);
        Log.e(TAG, "\n" + Formatter.formatIpAddress(wim.getConnectionInfo().getIpAddress()));

        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        String ip = Formatter.formatIpAddress(inetAddress.hashCode());
                        Log.i(TAG, "***** IP=" + ip);
                        return ip;
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e(TAG, ex.toString());
        }
        return null;
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public PeerListListener getPeerListListener() {
        return null;
    }

    @Override
    public void onPeersAvailable(WifiP2pDeviceList peers) {

    }

}